package com.zdlyh.utils;

import com.zdlyh.domain.entity.LoginUser;
import com.zdlyh.enums.AppHttpCodeEnum;
import com.zdlyh.exception.SystemException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {
    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser() {
        try{
            return (LoginUser) getAuthentication().getPrincipal();
        }catch (Exception e){
            throw new SystemException(AppHttpCodeEnum.NEED_TOLOGIN);
        }
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    //    获取userId
    public static Long getUserId() {
        return getLoginUser().getUser().getId();
    }

    public static Boolean isAdmin(){
        Long id = getLoginUser().getUser().getId();
        return id != null && 1L == id;
    }
}